package com.castle.fortress.admin.cms.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.cms.dto.CmsArticleDto;
import com.castle.fortress.admin.cms.entity.CmsArticleEntity;
import com.castle.fortress.admin.cms.service.CmsArticleService;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * cms内容管理表 api 控制器
 *
 * @author castle
 * @since 2022-03-10
 */
@Api(tags="cms内容管理表api管理控制器")
@RestController
@RequestMapping("/api/cms/cmsArticle")
public class ApiCmsArticleController {
	@Autowired
	private CmsArticleService cmsArticleService;


	/**
	 * cms内容管理表的分页展示
	 * @param cmsArticleDto cms内容管理表实体类
	 * @param currentPage 当前页
	 * @param size  每页记录数
	 * @return
	 */
	@ApiOperation("cms内容管理表分页展示")
	@GetMapping("/page")
	public RespBody<IPage<CmsArticleDto>> pageCmsArticle(CmsArticleDto cmsArticleDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
		Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
		Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
		Page<CmsArticleDto> page = new Page(pageIndex, pageSize);

		IPage<CmsArticleDto> pages = cmsArticleService.pageCmsArticle(page, cmsArticleDto);
		return RespBody.data(pages);
	}

	/**
	 * cms内容管理表保存
	 * @param cmsArticleDto cms内容管理表实体类
	 * @return
	 */
	@ApiOperation("cms内容管理表保存")
	@PostMapping("/save")
	public RespBody<String> saveCmsArticle(@RequestBody CmsArticleDto cmsArticleDto){
		if(cmsArticleDto == null ){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
			CmsArticleEntity cmsArticleEntity = ConvertUtil.transformObj(cmsArticleDto,CmsArticleEntity.class);
		if(cmsArticleService.save(cmsArticleEntity)){
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * cms内容管理表编辑
	 * @param cmsArticleDto cms内容管理表实体类
	 * @return
	 */
	@ApiOperation("cms内容管理表编辑")
	@PostMapping("/edit")
	public RespBody<String> updateCmsArticle(@RequestBody CmsArticleDto cmsArticleDto){
		if(cmsArticleDto == null || cmsArticleDto.getId() == null || cmsArticleDto.getId().equals(0L)){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
			CmsArticleEntity cmsArticleEntity = ConvertUtil.transformObj(cmsArticleDto,CmsArticleEntity.class);
		if(cmsArticleService.updateById(cmsArticleEntity)){
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * cms内容管理表删除
	 * @param ids cms内容管理表id集合
	 * @return
	 */
	@ApiOperation("cms内容管理表删除")
	@PostMapping("/delete")
	public RespBody<String> deleteCmsArticle(@RequestBody List<Long> ids){
		if(ids == null || ids.isEmpty()){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		if(cmsArticleService.removeByIds(ids)) {
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * cms内容管理表详情
	 * @param id cms内容管理表id
	 * @return
	 */
	@ApiOperation("cms内容管理表详情")
	@GetMapping("/info")
	public RespBody<CmsArticleDto> infoCmsArticle(@RequestParam Long id){
		if(id == null){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		CmsArticleDto cmsArticleDto =  cmsArticleService.getByIdExtends(id);
		return RespBody.data(cmsArticleDto);
	}


}
